<?php namespace App\Admin\Storefront\Schedule;

//基础条件
use App\Core\Repository\Criterias\FixedCriteria;
use App\Core\Repository\Criterias\PaginateCriteria;
use App\Core\Repository\Criterias\SortCriteria;
//过滤器
use App\Core\Repository\Criterias\Filters\InputCriteria;
use App\Core\Repository\Criterias\Filters\InputKeywordsCriteria;
use App\Core\Repository\Criterias\Filters\InputTimeRangeCriteria;

use DB;
use Input;
use App\Admin\Cabinet\CabinetCell\Repository as CabinetCellRepository;
use App\Admin\Cabinet\Cabinet\Repository as CabinetRepository;
use App\Admin\Experience\Experience\Repository as ExperienceRepository;

trait Service {
    
    //基础服务
    use \App\Core\Service\Service;
    use \App\Core\Service\Services\All;
    use \App\Core\Service\Services\Indexer;
    use \App\Core\Service\Services\Creater;
    use \App\Core\Service\Services\Updater;
    use \App\Core\Service\Services\Changer;
    use \App\Core\Service\Services\Deleter;
    use \App\Core\Service\Services\Detailer;
    //操作日志
    use \App\Admin\Base\LogService;
    
    protected function bootService(){
        $this->bootBaseService();
        $this->setRepository(new Repository);
        $this->setMeta(new Meta);
        $this->repository = $this->getRepository();
    }
	
    //列表
    public function lists(){
        //过滤器
        $conditions = [
            new InputKeywordsCriteria('name'),
            new InputTimeRangeCriteria('created_at'),
            new InputCriteria('status')
        ];
        $count = $this->repository->addCriterias($conditions)->count();
        $conditions[] = new PaginateCriteria();
        $conditions[] = new SortCriteria('id', 'DESC');
        $data = $this->repository->resetQuery()->resetCriteria()->addCriterias($conditions)->all();
        $data->load('storefront','cabinet');
        return $this->success([
            'count' => $count,
            'data' => $data
        ]);
    }

    public function updateGoodJsonFeild(){
        $id = Input::get('id');
        $goods_json = Input::get('goods_json');
        if($goods_json){
            $this->repository->addCriteria(new FixedCriteria('id', $id))->update([
                'goods_json' => $goods_json,
                'status' => '0'
            ]);
            return $this->success();
        }
        return $this->inputError('非法操作');
    }


    public function applicationx(){
        $id = Input::get('id');

        $data = $this->repository->addCriterias([
            new FixedCriteria('id', $id),
            new FixedCriteria('status', '0')
        ])->first();

        if(!$data || !$data->goods_json){
            return $this->inputError('非法操作');
        }

        $cabinetid = $data->cabinet_id;
        with(new ExperienceRepository())->addCriterias([new FixedCriteria('cabinet_id',$cabinetid),new FixedCriteria('status','1')])->update([
            'status' => '3'
        ]);

        $themeid = $data->theme_id;
        with(new CabinetRepository())->addCriteria(new FixedCriteria('id',$cabinetid))->update([
            'theme_id' => $themeid
        ]);

        $targetData = json_decode($data->goods_json);
        foreach($targetData as $item){
            with(new CabinetCellRepository())->addCriteria(new FixedCriteria('id', $item->id))->update([
                'goods_id' => $item->target ? $item->target->id : '0',
                'status' => '0'
            ]);
        };
        $data->status = '1';
        $data->save();
        return $this->success();
    }

    //未上线
    public function againonline(){
        return $this->change('status', '1', '2');
    }

    //上线
    public function online(){
        return $this->change('status', '1', '0');
    }

    //下线
    public function offline(){
        return $this->change('status', '2', '1');
    }

}